//
//  UIImage+Category.m
//  meiyinxiang
//
//  Created by apple on 2017/10/23.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "UIImage+Category.h"

@implementation UIImage (Category)
//(image:UIImage?) size:CGSize,backColor:UIColor? = UIColor.white
+ (UIImage *)hbj_AvatarIcon:(UIImage *)image
                       size:(CGSize)size
            backgroundColor:(UIColor*)backgroundColor{
    CGPoint point = CGPointMake(0, 0);
    CGRect rect = CGRectMake(point.x, point.y, size.width, size.height);
    /*
     1.size：绘图尺寸
     2.不透明 false（透明）/ true（不透明）
     3.scale：屏幕分辨率，默认生成的图像默认使用1.0的分辨率，乳香质量不好，可以指定0，会选择当前设备的屏幕分辨率
     
     */
    UIGraphicsBeginImageContextWithOptions(size, YES, 0);
    //背景填充
    [backgroundColor setFill];
    UIRectFill(rect);
    //2.实例化一个圆形路径
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:rect];
    //3.进行路径裁切
    [circlePath addClip];

    //4.绘图
    [image drawAsPatternInRect:rect];
    //绘制内切圆
    [UIColor.grayColor setStroke];
    circlePath.lineWidth = 1;
    [circlePath stroke];

    //5.获取上下文
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    //6.关闭上下文
    UIGraphicsEndImageContext();
    
    return resultImage;
}
@end
